package com.androidcourse.shounews.Adapter;

import static android.content.ContentValues.TAG;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.androidcourse.shounews.Entity.CommonNews;
import com.androidcourse.shounews.HttpUtil.NewsHttp;
import com.androidcourse.shounews.R;
import com.androidcourse.shounews.Toast.BaseToast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import java.util.List;

public class CommonNewsAdapter extends RecyclerView.Adapter<CommonNewsAdapter.CommonNewsViewHolder> implements View.OnClickListener {
    private List<CommonNews> newsList;
    private OnItemClickListener listener;
    public interface OnItemClickListener {
        void onNewsItemClick(int position);
    }
    @Override
    public void onClick(View v) {
        new BaseToast(v.getContext(), "点击了item");
    }

    public CommonNewsAdapter() {
    }
    public CommonNewsAdapter(List<CommonNews> newsList,OnItemClickListener listener) {
        this.newsList = newsList;
        this.listener=listener;
    }

    public class CommonNewsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView tv_title;
        private TextView tv_author;
        private TextView tv_time;
        public CommonNewsViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_title=itemView.findViewById(R.id.common_news_title);
            tv_author=itemView.findViewById(R.id.common_news_author);
            tv_time=itemView.findViewById(R.id.common_news_time);
            itemView.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            listener.onNewsItemClick(getAdapterPosition());
        }
    }
    @NonNull
    @Override
    public CommonNewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_view_common_item,parent,false);
        return new CommonNewsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommonNewsViewHolder holder, int position) {
        CommonNews commonNews=newsList.get(position);
        holder.tv_title.setText(commonNews.getTitle());
        holder.tv_author.setText(commonNews.getAuthor());
        holder.tv_time.setText(commonNews.getTime().replace('T',' '));
        Glide.with(holder.itemView.getContext())
                .load(NewsHttp.baseLocalUrl+NewsHttp.getImg+"?url="+commonNews.getImgUrl())
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        // 将Drawable转换为Bitmap，并创建圆角矩形
                        Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
                        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                        float radius = 20f; // 圆角半径
                        float[] radii = {radius, radius, radius, radius, radius, radius, radius, radius}; // 四个角的圆角半径
                        RoundRectShape shape = new RoundRectShape(radii, null, null);
                        ShapeDrawable drawable = new ShapeDrawable(shape);
                        drawable.getPaint().setShader(shader);
                        holder.itemView.setBackground(drawable);
                    }
                });
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }
}
